<template>
  <div class="main-out" ref="targetRef">
    <div>影像</div>
    <div class="item-display-container">
      <div
        v-for="(item, index) in baseMaps"
        :key="index"
        class="item"
        :class="{ 'item-selected': item.visible === true }"
        @click="selectBaseMap(index, baseMaps)"
      >
        <img class="item-image" :src="item.icon" :alt="item.tooltip" />
        <div class="item-text">
          {{ item.name }}
        </div>
      </div>
    </div>
    <div>地形</div>
    <div class="item-display-container">
      <div
        v-for="(item, index) in baseTerrains"
        :key="index"
        class="item"
        :class="{ 'item-selected': item.visible === true }"
        @click="selectBaseTerrain(index, baseTerrains)"
      >
        <img class="item-image" :src="item.icon" :alt="item.tooltip" />
        <div class="item-text">
          {{ item.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive } from 'vue';
import { useViewer } from '../stores/viewer';
import { useClickOutside } from '@/hooks/useClickOutside';

// 事件声明
const emits = defineEmits(['close']);
const outClick = () => {
  emits('close', {});
};

// 调用组合式函数，传入“关闭组件”的回调
const { targetRef } = useClickOutside(outClick);

const baseMaps = reactive([
  {
    name: '网络地图  (试验)',
    icon: './static/img/basemaps/google_img.png',
    type: 'tile',
    crs: '4490',
    url: 'https://lyzzsoft500.obs.cn-north-1.myhuaweicloud.com/mapdata/gm/{z}/{x}/{y}.jpg',
    minimumLevel: 1,
    maximumLevel: 14,
    tooltip: '试验用中国国内地图数据',
    boundingBox: {
      xMin: -180,
      xMax: 180,
      yMin: -85,
      yMax: 85
    },
    visible: false
  }
]);
const baseTerrains = reactive([
  {
    type: 'ellipsoid',
    name: '椭球体',
    tooltip: '84椭球体',
    icon: './static/img/basemaps/TerrainEllipsoid.png',
    visible: true
  }
]);

onMounted(() => {
  baseMaps.length = 0;
  const viewer = useViewer().viewer;
  viewer.zz.config.basemaps.forEach(item => {
    baseMaps.push(Object.assign({}, item));
  });

  baseTerrains.length = 0;
  viewer.zz.config.baseterrains.forEach(item => {
    baseTerrains.push(Object.assign({}, item));
  });
});

// 方法
const selectBaseMap = (index, arr) => {
  // 修改前台显示
  for (let i = 0; i < arr.length; ++i) {
    if (i == index) {
      arr[i].visible = true;
    } else {
      arr[i].visible = false;
    }
  }

  // 修改地图数据
  const viewer = useViewer().viewer;
  const config = viewer.zz.config.basemaps;
  for (let i = 0; i < config.length; ++i) {
    if (i == index) {
      config[i].visible = true;
    } else {
      config[i].visible = false;
    }
  }
  viewer.zz._settingOptions.setBaseImagery(config[index]);
};

const selectBaseTerrain = (index, arr) => {
  for (let i = 0; i < arr.length; ++i) {
    if (i == index) {
      arr[i].visible = true;
    } else {
      arr[i].visible = false;
    }
  }

  const viewer = useViewer().viewer;
  const config = viewer.zz.config.baseterrains;
  for (let i = 0; i < config.length; ++i) {
    if (i == index) {
      config[i].visible = true;
    } else {
      config[i].visible = false;
    }
  }
  viewer.zz._settingOptions.setBaseTerrain(config[index]);
};
</script>

<style scoped>
.main-out {
  position: absolute;
  top: 40px;
  right: 5px;
  border-radius: 6px;
  background-color: rgba(0, 0, 0, 0.5);
  padding: 5px;
  overflow-y: auto;
  max-height: 80%;
}
.item-display-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
  max-width: 520px;
  padding: 5px;
  border: 1px solid #aaa;
}

.item {
  position: relative;
  width: 100px;
  cursor: pointer;
  transition: all 0.3s ease;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.2);
  padding: 5px;
  border-radius: 4px;
}

.item:hover {
  background-color: rgba(0, 0, 0, 0.7);
}

.item-selected {
  background-color: rgba(0, 0, 0, 0.5);
}

.item-image {
  width: 90px;
  height: 90px;
  overflow: hidden;
  object-fit: cover;
  border-radius: 4px;
}

.item:hover .item-image {
  transform: scale(1.1);
}

.item-text {
  text-align: center;
  font-size: 14px;
}

.item-selected .item-text {
  color: #00eeffda;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .item-display-container {
    flex-direction: column;
    align-items: center;
  }

  .item {
    width: 80%;
    max-width: 200px;
  }
}
</style>
